package cn.wind.xboot.core.utils;

import cn.hutool.core.util.ReUtil;
import com.google.common.collect.Maps;
import lombok.NonNull;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author xukk
 * @date 2018/5/4
 */
public class ExpressionUtil {
    private static ExpressionParser parser = new SpelExpressionParser();

    public static String parseExpression(@NonNull String str, @NonNull List list) {
        Map<Integer, Object> map = Maps.newHashMap();
        for (int i = 0; i < list.size(); i++) {
            map.put(i, list.get(i));
        }
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("map", map);
        context.setRootObject(map);
        Expression randomPhrase;
        if(ReUtil.isMatch(".*#\\{.*\\}.*",str)){
            randomPhrase = parser.parseExpression(
                    str,new TemplateParserContext());
        }else {
            randomPhrase = parser.parseExpression(str);
        }
       return randomPhrase.getValue(context, String.class);
    }
    public static <T>T parseExpression(@NonNull String str, @NonNull List list,Class<T> c) {
        Map<Integer, Object> map = Maps.newHashMap();
        for (int i = 0; i < list.size(); i++) {
            map.put(i, list.get(i));
        }
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("map", map);
        context.setRootObject(map);
        Expression randomPhrase;
        if(ReUtil.isMatch(".*#\\{.*\\}.*",str)){
            randomPhrase = parser.parseExpression(
                    str,new TemplateParserContext());
        }else {
            randomPhrase = parser.parseExpression(str);
        }
        return randomPhrase.getValue(context,c);
    }
    public static <T>T parseExpression(@NonNull String str, @NonNull Map<String,Object> objectMap,Class<T> c) {
        StandardEvaluationContext context = new StandardEvaluationContext();
        objectMap.forEach((k,v)-> context.setVariable(k, v));
        Expression randomPhrase;
        if(ReUtil.isMatch(".*#\\{.*\\}.*",str)){
            randomPhrase = parser.parseExpression(
                    str,new TemplateParserContext());
        }else {
            randomPhrase = parser.parseExpression(str);
        }
            return randomPhrase.getValue(context,c);

    }
    public static Object parseExpression(@NonNull String str, @NonNull Map<String,Object> objectMap) {
        StandardEvaluationContext context = new StandardEvaluationContext();
        objectMap.forEach((k,v)-> context.setVariable(k, v));
        Expression randomPhrase;
        if(ReUtil.isMatch(".*#\\{.*\\}.*",str)){
            randomPhrase = parser.parseExpression(
                    str,new TemplateParserContext());
        }else {
            randomPhrase = parser.parseExpression(str);
        }
        return randomPhrase.getValue(context);
    }

    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("22");
        System.out.println(parseExpression("感谢您使用金米袋，注册验证码为：#{[0]}，有效时间为5分钟。【金米袋】",
             list,String.class));
    }
}
